import {computed, reactive, ref} from "vue";
import {VerifyEnrollDAT} from "../../types/responseTypes";
import {getVerifyEnrollList} from "../../api/enroll";
import {useStore} from "vuex";
export function useVerifyEnrollList(){

    const enrollListWrap = reactive<{enrollVerifyList: VerifyEnrollDAT[]}>({
        enrollVerifyList: []
    })

    const total = ref(0)
    const {state} = useStore()

    function initVerifyEnrollList(page: number = 1, pageSize: number = 20, params?: Partial<{
        SH_FLAG: '0' | '1' | '2'
        CLASSNAME: string
        STUDENTNAME: string
    }>){
        return new Promise((resolve, reject) => {
            getVerifyEnrollList({
                CLASSNAME: params?.CLASSNAME || '',
                STUDENTNAME: params?.STUDENTNAME || '',
                SH_FLAG: params?.SH_FLAG || '0',
                PAGESIZE: pageSize,
                PAGEINDEX: page,
                OPERATOR_ID: state.user.userInfo.CUSER_ID
            }).then(res => {
                if (res.ERROR[0].errcode == '0'){
                    enrollListWrap.enrollVerifyList = JSON.parse(JSON.stringify(res.DAT))
                    total.value = res.COUNT[0].NUM
                    resolve(null)
                }else {
                    uni.showToast({
                        title: res.ERROR[0].error,
                        icon: 'none',
                        mask: true
                    })
                    reject('err')
                }
            }).catch(err => {
                uni.showToast({
                    title: '获取列表失败',
                    icon: 'error',
                    mask: true
                })
                reject('err')
            })
        })
    }

    return {
        enrollVerifyList: computed(() => enrollListWrap.enrollVerifyList),
        total,
        initVerifyEnrollList
    }
}
